package cn.edu.scau.cmi.zhengweibo.comprehension.client;
import java.util.Scanner;

import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.zhengweibo.comprehension.dao.StudentDAO;
import cn.edu.scau.cmi.zhengweibo.comprehension.dao.TeacherDAO;
import cn.edu.scau.cmi.zhengweibo.comprehension.domain.Student;
import cn.edu.scau.cmi.zhengweibo.comprehension.domain.Teacher;
import cn.edu.scau.cmi.zhengweibo.comprehension.util.ComprehensionApplicationContextUtil;

public class ComprehensionConsoleClient {
//	（1） 初始化applicationContext，并获取studentDAO对象
	public static ApplicationContext applicationContext = ComprehensionApplicationContextUtil.getApplicationContext();
	public static StudentDAO studentDAO=(StudentDAO) applicationContext.getBean("studentDAO");
	public static TeacherDAO teacherDAO=(TeacherDAO) applicationContext.getBean("teacherDAO");
	public static Scanner scanner = new Scanner(System.in);
	static String id = null;
	static String teaId = null;
	static String teacherName=null;
	static String studentName=null;
	static Student student=null;
	static Teacher teacher=null;
	public static void main(String[] args) {
		
		while(true) {
			System.out.println("1、学生模块");
			System.out.println("2、老师模块");
			System.out.println("3、管理员模块");
			System.out.println("4、退出");
			System.out.print("请输入相应序号:");
			switch(scanner.nextLine()) {
			   case "1": 
				   stuModel();
				   break;
			   case "2":
				   teaModel();
				   break;
			   case "3":
				   manageModel();
				   break;
			   case "4":
				   System.exit(0);
			   default:
				   break;
			}
		}
	}
	
	public static void stuModel() {
		while(true) {
			
			System.out.println("1、选择指导教师");
			System.out.println("2、修改指导教师");
			System.out.println("3、退出");
			System.out.print("请选择：");
			
			switch(scanner.nextLine()) {
				case "1":
					System.out.print("请输入你的姓名：");
					studentName=scanner.nextLine();
					student = (Student) studentDAO.findByName(studentName).get(0);
					System.out.println(student.getName());
					if(student == null) {
						student = new Student();
						System.out.println("没有相关信息，请输入你的姓名：");
						studentName = scanner.nextLine();
						student.setName(studentName);
					}
					System.out.print("请输入指导老师：");
					teacherName = scanner.nextLine();
					teacher = (Teacher) teacherDAO.findByName(teacherName).get(0);
					student.setTeacher(teacher);
					studentDAO.merge(student);
					
					System.out.println("选择成功!");
					break;
				case "2":
					System.out.print("请输入你的姓名：");
					studentName=scanner.nextLine();
					student = (Student) studentDAO.findByName(studentName).get(0);
					if(student == null) {
						student = new Student();
						System.out.print("没有相关信息，请输入你的姓名：");
						studentName = scanner.nextLine();
						student.setName(studentName);
					}
					System.out.print("请输入指导老师：");
					teacherName = scanner.nextLine();
					teacher = (Teacher) teacherDAO.findByName(teacherName).get(0);
					student.setTeacher(teacher);
					studentDAO.merge(student);
					System.out.println("修改成功!");
					break;
				case "3":
					System.exit(0);
				default:
					break;
			}
		}
		
	}
	
	public static void teaModel() {
		while(true) {
			
			System.out.println("1、选择被指导的学生");
			System.out.println("2、修改被指导的学生");
			System.out.println("3、退出");
			System.out.print("请选择：");
			switch(scanner.nextLine()) {
			case "1":
				System.out.print("请输入您的姓名：");
				teacherName = scanner.nextLine();
				teacher = (Teacher) teacherDAO.findByName(teacherName).get(0);
				if(teacher == null) {
					teacher = new Teacher();
					System.out.print("没有相关信息，请输入你的姓名：");
					teacherName = scanner.nextLine();
					teacher.setName(teacherName);
				}
				System.out.print("请输入指导的学生：");
				studentName = scanner.nextLine();
				student = (Student) studentDAO.findByName(studentName).get(0);
				student.setTeacher(teacher);
				studentDAO.merge(student);
				System.out.println("选择成功!");
				break;
			case "2":
				System.out.print("请输入您的姓名：");
				teacherName = scanner.nextLine();
				teacher = (Teacher) teacherDAO.findByName(teacherName).get(0);
				if(teacher == null) {
					teacher = new Teacher();
					System.out.print("没有相关信息，请输入你的姓名：");
					teacherName = scanner.nextLine();
					teacher.setName(teacherName);
				}
				System.out.print("请输入指导的学生：");
				studentName = scanner.nextLine();
				student = (Student) studentDAO.findByName(studentName).get(0);
				student.setTeacher(teacher);
				studentDAO.merge(student);
				System.out.println("修改成功!");
				break;
			case "3":
				System.exit(0);
			default:
				break;
			}
		}
	}
	
	public static void manageModel() {
		while(true) {
			
			System.out.println("1、添加学生");
			System.out.println("2、修改学生");
			System.out.println("3、删除学生");
			System.out.println("4、添加老师");
			System.out.println("5、修改老师");
			System.out.println("6、删除老师");
			System.out.println("7、退出");
			System.out.print("请选择：");
			switch(scanner.nextLine()) {
			case "1":
				System.out.print("请输入你的姓名：");
				studentName = scanner.nextLine();
				System.out.print("请输入你的导师：");
				teacherName = scanner.nextLine();
				student = new Student();
				student.setName(studentName);
				if(teacherName!=null||teacherName != "") {
					teacher = (Teacher) teacherDAO.findByName(teacherName).get(0);
				}
				student.setTeacher(teacher);
				studentDAO.save(student);
				System.out.println("添加成功!");
				break;
			case "2":
				System.out.print("请输入你的姓名：");
				studentName = scanner.nextLine();
				student = (Student) studentDAO.findByName(studentName).get(0);
				if(student == null) {
					System.out.println("不存在该学生");
					break;
				}
				System.out.print("请添加你的姓名：");
				studentName = scanner.nextLine();
				student.setName(studentName);
				studentDAO.merge(student);
				System.out.println("修改成功!");
				break;
			case "3":
				System.out.print("请输入要删除的学生姓名：");
				studentName = scanner.nextLine();
				student = (Student) studentDAO.findByName(studentName).get(0);
				if(student != null) {
					studentDAO.delete(student);
				}
				System.out.println("删除成功!");
				break;
			case "4":
				System.out.print("请输入您的姓名：");
				teacherName = scanner.nextLine();
				teacher = new Teacher();
				teacher.setName(teacherName);
				
				teacherDAO.save(teacher);
				System.out.println("添加成功!");
				break;
			case "5":
				System.out.print("请输入你的姓名：");
				teacherName = scanner.nextLine();
				teacher = (Teacher) teacherDAO.findByName(teacherName).get(0);
				if(teacher == null) {
					System.out.println("不存在该老师");
					break;
				}
				System.out.print("请输入要修改的姓名：");
				teacherName = scanner.nextLine();
				teacher.setName(teacherName);
				teacherDAO.merge(teacher);
				System.out.println("修改成功!");
				break;
			case "6":
				System.out.print("请输入要删除的老师：");
				teacherName = scanner.nextLine();
				teacher =(Teacher) teacherDAO.findByName(teacherName).get(0);
				
				if(teacher != null) {
					teacherDAO.delete(teacher);
				}
				System.out.println("删除成功!");
				break;
			case "7":
				System.exit(0);
			default:
				break;
			}
		}
	}
}